package org.s166.stu.action;

import com.google.gson.Gson;
import org.s166.stu.dao.StudenDao;
import org.s166.stu.entity.Student;
import org.s166.stu.factories.Factories;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/login.do")
public class Login extends BaseServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        String pass = req.getParameter("pass");
        System.out.println(id+"   -------   "+pass);
        StudenDao dao = Factories.getStudentDao();
        Student stu = dao.login(id, pass);
        Gson gson=new Gson();
        if(stu!=null){
            resp.getWriter().print(gson.toJson(successJson(stu,200,"登录成功")));
        }else{
            resp.getWriter().print(gson.toJson(errorJson(500,"帐号或密码错误")));
        }
        resp.getWriter().flush();
        resp.getWriter().close();
    }
}
